package com.ld.xuanKe.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ProjectName xuanKe
 * @ClassName TimestampStringConverter
 * @Author 刘邓
 * @Date 2023-12-10 14:23
 */
public class TimestampStringConverter implements Converter<Timestamp> {

	public Class<?> supportJavaTypeKey() {
		return Timestamp.class;
	}


	public CellDataTypeEnum supportExcelTypeKey() {
		return CellDataTypeEnum.STRING;
	}



	public WriteCellData<?> convertToExcelData(Timestamp value, ExcelContentProperty contentProperty,
	                                           GlobalConfiguration globalConfiguration) {
		WriteCellData cellData = new WriteCellData();
		String cellValue;
		if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
			cellValue = DateUtils.format(value.toLocalDateTime(), "yyyy年MM月dd日 HH时mm分", globalConfiguration.getLocale());
		} else {
			cellValue = DateUtils.format(value.toLocalDateTime(), contentProperty.getDateTimeFormatProperty().getFormat(),
					globalConfiguration.getLocale());
		}
		cellData.setType(CellDataTypeEnum.STRING);
		cellData.setStringValue(cellValue);
		cellData.setData(cellValue);
		return cellData;
	}
}
